# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0
from selenium.webdriver.common.action_chains import ActionChains
import unittest
import random
import time
from cookies import lasthopes, randomword
import re
import string
import sys
from selenium.webdriver.common.keys import Keys
from PIL import Image
from os import remove, listdir, sep
from os.path import basename, isfile
import tempfile
from warnings import warn
from basecart import BaseCart
import json

class cart_mass_good(BaseCart):
    def test_cart_mass_good(self):
        driver = self.driver
        self.get_cat()
        self.get_item()
        #First item for entring to cart
        self.buy_item()
        txt = ''
        separators = ['', ', ', ' ', '\n']
        #Massive collect
        for sep in separators:
            self.get_cat()
            itm = random.choice(WebDriverWait(driver, timeout=10).until(lambda dr: dr\
                                              .find_elements_by_css_selector("div._b-product-item")))
            itmid = json.loads(itm.get_attribute("data-conf-render-data"))['id']
            txt = txt+sep+itmid
            print txt
        self.go_cart()
        mass_ops = WebDriverWait(driver, timeout=10).until(lambda dr: [a for a in
                                                dr.find_elements_by_css_selector("span.g-pseudo-h") if\
                                                               a.get_attribute("textContent")==u'Массовые операции'])[0]
        mass_ops.click()
        mass_add = WebDriverWait(driver, timeout=10).until(lambda dr: [a for a in
                                                dr.find_elements_by_css_selector("span.g-pseudo-h") if\
                                                               a.get_attribute("textContent")==u'Добавление товаров'])[0]
        mass_add.click()
        mass_add_tlt = WebDriverWait(driver, timeout=10).until(lambda dr: dr.find_element_by_css_selector("div.b-tooltip-cart-actions"))
        mass_add_inp = WebDriverWait(mass_add_tlt, timeout=10).until(lambda dr: dr.find_element_by_css_selector("textarea.b-input-field"))
        mass_add_inp.clear()
        mass_add_inp.send_keys(txt)
        mass_add_btn = mass_add_tlt.find_element_by_css_selector("input.b-sbutton-input")
        mass_add_btn.click()
        try: 
            self.find_error_tooltip(driver, '')
        except:
            WebDriverWait(mass_add_tlt, timeout=10).until(lambda dr: self.cart_items_ontop()==len(separators))
            #+First item
            assert self.cart_items_ontop()==len(separators)+1,\
            'Count of items on top not equals count of items in the cart'
